﻿using System;
using System.Configuration;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.Common.Locator;

using Fuse8.DomainFramework.SubsonicPlus.DataAccess;

namespace Fuse8.DomainFramework.SubsonicPlus
{
    public static class SubSonicRepositoryPolicy<TEntity, TKey> 
        where TEntity : class, IEntityObject<TKey>, new()
        where TKey : IComparable<TKey>, IEquatable<TKey> 
    {
        #region Properties

        internal static string SqlConnectionAlias
        {
            get
            {
                return ConfigurationManager.AppSettings["DataConnectionAlias"] != null ?
                    ConfigurationManager.AppSettings["DataConnectionAlias"] :
                    string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string SqlDataConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings[SqlConnectionAlias] != null ?
                    ConfigurationManager.ConnectionStrings[SqlConnectionAlias].ConnectionString :
                    string.Empty;
            }
        }

        public static string SqlDataProviderType
        {
            get
            {
                return ConfigurationManager.ConnectionStrings[SqlConnectionAlias] != null ?
                        ConfigurationManager.ConnectionStrings[SqlConnectionAlias].ProviderName :
                        string.Empty;
            }
        } 

        #endregion

        public static void RegisterEntityType()
        {
            RegisterDependencyContainer.Instance.RegisterType<IEntityDataRepository<TEntity, TKey>, SubSonicEntityDataRepository<TEntity, TKey>>(
                SqlDataConnectionString, SqlDataProviderType);
        }
    }
}
